using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Xml;

namespace CustomCMS.API.Util
{
    public class ConfigurationHandler : IConfigurationSectionHandler
    {
        #region IConfigurationSectionHandler Members

        public static string Version = "1.1.7";	

        private string _repository;

        public string Repository
        {
            get { return _repository; }
        }

        private string _persistanceProvider;

        public string PersistanceProvider
        {
            get { return _persistanceProvider; }
        }

		private Dictionary<string,ObjectModel.User> _users;

		public Dictionary<string,ObjectModel.User> Users
		{
			get { return _users; }
			set { _users = value; }
		}

        private string _exportPath;

        public string ExportPath
        {
            get { return _exportPath; }
            set { _exportPath = value; }
        }

        private string _exportDomain;

        public string ExportDomain
        {
            get { return _exportDomain; }
            set { _exportDomain = value; }
        }
	
		private XmlNode _packaging;

		public XmlNode Packaging {
			get { return _packaging; }
		}

		private string _zip;
        private string _unzip;
        private string _targetZipFilename;

        public string Zip
        {
            get { return _zip; }
        }

        public string Unzip
        {
            get { return _unzip; }
        }

        public string TargetZipFilename
        {
            get { return _targetZipFilename; }
        }

        public object Create(object parent, object configContext, System.Xml.XmlNode section)
        {
            SetVal(ref _repository, section, "repository");
            SetVal(ref _persistanceProvider, section, "persistanceProvider");
            SetVal(ref _exportPath, section, "exportPath");
            SetVal(ref _exportDomain, section, "exportDomain");

			XmlNodeList users = GetVals(section, "users/user");
			_users = new Dictionary<string,CustomCMS.API.ObjectModel.User>();
			foreach (XmlNode user in users) {
				string username = user.Attributes["name"].Value;
				string password = user.Attributes["password"].Value;
				string actions = user.Attributes["actions"].Value;

				ObjectModel.User oUser = new CustomCMS.API.ObjectModel.User(username, password, actions);

				_users.Add(username, oUser);
			}

			_packaging = section.SelectSingleNode("packaging");

            SetVal(ref _zip, section, "zipTool/zip");
            SetVal(ref _unzip, section, "zipTool/unzip");
			SetVal(ref _targetZipFilename, section, "zipTool/targetZipFilename");

            return this;
        }

		private void SetVal(ref string var, XmlNode section, string path) {
			XmlNode node = section.SelectSingleNode(path);
			if (node == null) throw new Exception("Item " + path + " is missing from Web.Config");
			var = node.InnerText;
		}

		private XmlNodeList GetVals(XmlNode section, string path) {
			XmlNodeList nodes = section.SelectNodes(path);
			if (nodes == null) throw new Exception("Item " + path + " is missing from Web.Config");
			return nodes;
		}

		private Dictionary<string, string> GetDict(XmlNode section, string path, string key, string value) {
			XmlNodeList list = GetVals(section, path);
			Dictionary<string, string> dict = new Dictionary<string, string>();
			foreach (XmlNode item in list)
				try { dict.Add(item.Attributes[key].InnerText, item.Attributes[value].InnerText); } 
				catch (Exception) { throw new Exception("One of the " + path + " items is missing '" + key + "' or '" + value + "' attributes"); }
			return dict;
		}

        #endregion
    }
}
